package com.itheima.wms.service.impl;

import com.itheima.wms.mapper.AreaMapper;
import com.itheima.wms.mapper.WareHouseMapper;
import com.itheima.wms.pojo.*;
import com.itheima.wms.service.AreaService;
import com.itheima.wms.utils.PageBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class AreaServiceImpl implements AreaService {

    private final AreaMapper areaMapper;
    private final WareHouseMapper wareHouseMapper;

    @Override
    public PageBean queryAreaPageByCondition(AreaPageRequest areaPageRequest) {

        Long total = areaMapper.queryAreaCount();

        Integer current = areaPageRequest.getCurrent();

        Integer pageSize = areaPageRequest.getSize();

        Integer start = (current - 1) * pageSize;

        List<Area> areas = areaMapper.queryAreaPageByCondition(start, pageSize, areaPageRequest);

        PageBean pageBean = new PageBean(areas, total, pageSize);

        return pageBean;
    }

    @Override
    public Area queryAreaById(Long id) {
        Area area = areaMapper.queryAreaById(id);
        return area;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertArea(Area area) {
        areaMapper.insertArea(area);
        Long warehouseId = area.getWarehouseId();
        wareHouseMapper.plusIncludeNum(warehouseId);
    }

    @Override
    @Transactional
    public void updateArea(Area area) {
        if (area.getWarehouseId() == null) {
            areaMapper.stopAndStartArea(area);
        } else {
            Long oldId = areaMapper.getOldId(area);
            Long newId = area.getWarehouseId();
            areaMapper.updateArea(area);
            if (!oldId.equals(newId)) {
                wareHouseMapper.minusIncludeNum(oldId);
                wareHouseMapper.plusIncludeNum(newId);
            }
        }
    }

    @Override
    @Transactional
    public boolean removeByIds(List<Long> ids) {
        Long id = ids.get(0);
        Integer includedNum = areaMapper.queryAreaById(id).getIncludedNum();
        if (includedNum <= 0) {
            Area area = new Area();
            area.setId(id);
            Long oldId = areaMapper.getOldId(area);
            wareHouseMapper.minusIncludeNum(oldId);
            areaMapper.removeByIds(id);
            return true;
        }
        return false;
    }

    @Override
    public List<Area> queryareaList(AreaDto areaPageRequest) {
        List<Area> areas = areaMapper.queryareaList(areaPageRequest);
        return areas;
    }

    @Override
    public void plusIncludeNum(Long id) {
        areaMapper.plusIncludeNum(id);
    }

    @Override
    public void minusIncludeNum(Long id) {
        areaMapper.minusIncludeNum(id);
    }

    @Override
    public AreaNumVo countIsUseOrFree(Long id) {
        List<LocationVO> locationVoList = areaMapper.countIsUseOrFree(id);
        int total = 0;
        int free = 0;
        int use = 0;
        int stop = 0;
        for (LocationVO locationVo : locationVoList) {
            if (locationVo.getStatus() == 0) {
                stop++;
            } else if (locationVo.getStatus() == 1) {
                if (locationVo.getUseStatus() == 0) {
                    free++;
                } else if (locationVo.getUseStatus() == 1) {
                    use++;
                }
            }
        }
//        AreaNumVo areaNumVo = new AreaNumVo(total, stop, use, free);
        AreaNumVo areaNumVo = new AreaNumVo();
        areaNumVo.setFree(free);
        areaNumVo.setUse(use);
        areaNumVo.setStop(stop);
        areaNumVo.setTotal(total);

        return areaNumVo;
    }
}
